<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
            <a-col :md="8" :sm="12">
                <a-form-item
                        label="项目编号"
                >
                    <a-input v-model="queryParam.xmbh"/>
                </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
                <a-form-item
                        label="项目名称"
                >
                    <a-input v-model="queryParam.xmmc"/>
                </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
                <a-form-item label="项目类型">
                    <a-select allowClear v-model="queryParam.xmlxm" @change="yxChange" showSearch
                              :filterOption="filterOption">
                        <a-select-option v-for="i in HWXNYJYDSXMLXM" :key="i.id" :value="i.code">{{ i.name }}
                        </a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>

          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item label="项目类别">
                <a-select allowClear v-model="queryParam.xmlbm" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in HWXNYJYDSXMLBM" :key="i.id" :value="i.code">{{ i.name }}
                  </a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="10" :sm="20">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 8px">{{ $t('button.query') }}</a-button>
              <a-button type="primary" @click="openCheck('add')">新增项目</a-button>
              <a-button type="primary" style="margin:0 8px" @click="batchExport">{{ $t('button.export') }}</a-button>
              <!-- <a-button type="primary"  @click="modelExport">导出申请表</a-button> -->
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? $t('button.packUp') : $t('button.advance') }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :alert="options.alert"
        :columns="columns"
        :data="loadData"
        :pagination="{ pageSize: 10 }"
        :rowSelection="options.rowSelection"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="qzsj" slot-scope="a,b">{{b.kssj}}--{{b.jssj}}</span>
         <template slot="operation" slot-scope="a,b">
            <a @click="openCheck('detail',b)">查看详情</a>
            <a v-if="b.shzt == '01' " @click="pushDetile(b)">提交</a>
            <a v-if="b.shzt == '23' && b.xmlxmc == '国际交流项目'" @click="modelExport(b)">打印</a>
            <a v-if="b.shzt == '01' || b.shzt == '22' || b.shzt == '24' || b.shzt == '23'" @click="openCheck('xg',b)">修改</a>
        </template>
      </s-table>
    </a-card>
    <project-add
      ref="check"
      :visible="visible"
      :type="type"
      :id="id"
      :b="bList"
      @cancel="modelCancel"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import moment from 'moment';
import projectAdd from './add/index'

const selectArr = ['HWXNYJYDSXMLXM']
export default {
  name: 'project',
  data() {
    return {
      advanced: true,
      queryParam: {},
      visible:false,
      type: '',
      id:'',
      bList:{},
      columns: [
          {
              title: '序号',
              width: '80px',
              scopedSlots: {customRender: 'serial'}
          },
          {
              title: '所在院系',
              dataIndex: 'ssyxmc'
          },
          {
              title: '项目编号',
              dataIndex: 'xmbh'
          },
          {
              title: '项目名称',
              dataIndex: 'xmmc'
          },
          {
              title: '项目类别',
              dataIndex: 'xmlbmc'
          },
          {
              title: '项目起止时间',
              dataIndex: 'kssj',
          scopedSlots: { customRender: 'qzsj' },
        },
        {
          title: '项目类型',
          dataIndex: 'xmlxmc',
          width:160
        },
        {
          title: '审核状态',
          width: '150px',
          dataIndex: 'shztmc'
        },
        {
          title: this.$t('key.option'),
          width: '180px',
          dataIndex: 'id',
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign({}, this.queryParam, parameter)
        if (params.fbsj) {
          params.fbsj = params.fbsj.format('YYYY-MM')
        }
        return this.$api.overseas.projectList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {},
      detailVisible: false,
      confirmLoading: false,
      courseInfo: {},
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
    }
  },
  components: { STable , projectAdd},
  computed: {
    ...mapState({
      TYPE0126: state => state.app['TYPE0126'], // 年级
      TYPE0075: state => state.app['TYPE0075'], // 学生类别
      HWXNYJYDSXMLXM: state => state.app['HWXNYJYDSXMLXM'],
      HWXNYJYDSXMLBM: state => state.app['HWXNYJYDSXMLBM'],
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    modelCancel(){
      this.visible = false
      this.$refs.table.refresh(true)
    },
    filterOption (value, yx) {
      return yx.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'sszym', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
          this.queryParam.sszym = ""
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    openCheck(type,value) {
      if(value){
        this.id = value.id
      }else{
        this.id = ""
      }
      this.visible = true;
      this.type = type;
      this.bList = value
    },
    pushDetile(value){
      value.shzt = '02'
      this.$api.overseas.addProject(value).then(res => {
          this.$message.success('提交成功', 4)
          this.$refs.table.refresh(true)
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    batchExport() {
      if (!this.selectedRowKeys.length) {
        return this.$message.warn('请选择要导出的项目信息')
      }
      console.log(this.selectedRowKeys)
      this.$export('/teacherClient/xn/dsxm/exportAll/'+this.selectedRowKeys.join(','), {

      })
    },
    modelExport(b) {
      this.$export('/teacherClient/xn/dsxm/exportWord/' + b.id, {

      })
    },
  }
}
</script>

